package cn.tqfeiyang.springplus.warehouse.controller;

import cn.tqfeiyang.springplus.warehouse.service.CompanyService;
import cn.tqfeiyang.springplus.warehouse.request.CompanyDTO;
import cn.tqfeiyang.springplus.warehouse.response.CompanyVO;
import cn.tqfeiyang.springplus.warehouse.request.CompanySTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.tqfeiyang.springplus.framework.response.ApiResult;
import cn.tqfeiyang.springplus.framework.pagination.PageData;
import cn.tqfeiyang.springplus.framework.validation.WhenCreate;
import cn.tqfeiyang.springplus.framework.validation.WhenUpdate;
import cn.tqfeiyang.springplus.framework.validation.EntityId;
import cn.tqfeiyang.springplus.framework.security.JwtSecurityRequirement;
import cn.tqfeiyang.springplus.framework.logging.LoggingRequirement;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;

/**
 * 公司前端控制器
 *
 * @author tqfeiyang
 * @since 2024-08-01
 */
@Slf4j
@RestController
@Validated
@RequestMapping("/api/warehouse/company")
@Tag(name = "基础/公司")
public class CompanyController {

    /**
     * 公司服务
     */
    @Resource
    private CompanyService companyService;

    /**
     * 查询公司
     *
     * @param companySTO 查询数据
     * @return 公司列表
     */
    @GetMapping("/query")
    @Operation(summary = "查询公司")
    @PreAuthorize("hasAnyAuthority('warehouse:company:query')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<PageData<CompanyVO>> selectCompanyPage(CompanySTO companySTO) {
        return ApiResult.success(companyService.selectCompanyPage(companySTO));
    }

    /**
     * 读取公司
     *
     * @param companyId 公司id
     * @return 公司信息
     */
    @GetMapping("/read")
    @Operation(summary = "读取公司")
    @PreAuthorize("hasAnyAuthority('warehouse:company:read')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<CompanyVO> selectCompanyInfo(@RequestParam @EntityId Long companyId) {
        return ApiResult.success(companyService.selectCompanyInfo(companyId));
    }

    /**
     * 新建公司
     *
     * @param companyDTO 请求数据
     * @return 公司信息
     */
    @PostMapping("/create")
    @Operation(summary = "新建公司")
    @PreAuthorize("hasAnyAuthority('warehouse:company:create')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<CompanyVO> createCompany(@RequestBody @Validated(WhenCreate.class) CompanyDTO companyDTO) {
        Long companyId = companyService.createCompany(companyDTO);
        return ApiResult.success(companyService.selectCompanyInfo(companyId));
    }

    /**
     * 修改公司
     *
     * @param companyId 公司id
     * @param companyDTO 请求数据
     * @return 公司信息
     */
    @PostMapping("/update")
    @Operation(summary = "修改公司")
    @PreAuthorize("hasAnyAuthority('warehouse:company:update')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<CompanyVO> updateCompany(@RequestParam @EntityId Long companyId, @RequestBody @Validated(WhenUpdate.class) CompanyDTO companyDTO) {
        companyService.updateCompany(companyId, companyDTO);
        return ApiResult.success(companyService.selectCompanyInfo(companyId));
    }

    /**
     * 删除公司
     *
     * @param companyId 公司id
     */
    @PostMapping("/delete")
    @Operation(summary = "删除公司")
    @PreAuthorize("hasAnyAuthority('warehouse:company:delete')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult deleteCompany(@RequestParam @EntityId Long companyId) {
        companyService.deleteCompany(companyId);
        return ApiResult.success();
    }
}
